package cc.sjfx.sharedoc.activity.user;

import cn.bmob.v3.listener.SaveListener;
import cn.mandroid.frame.toast.MToast;
import cn.mandroid.frame.tools.MD5;
import cc.sjfx.sharedoc.R;
import cc.sjfx.sharedoc.activity.BasicActivity;
import cc.sjfx.sharedoc.bmob.SessionToken;
import cc.sjfx.sharedoc.db.bmob.User;
import cc.sjfx.sharedoc.utils.CheckUtils;
import cc.sjfx.sharedoc.utils.StrUtils;
import cc.sjfx.sharedoc.widget.anotation.ViewInject;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class UserLoginActivity extends BasicActivity implements OnClickListener {
	@ViewInject(id = R.id.regText)
	private TextView regText;
	@ViewInject(id = R.id.phoneEdit)
	private EditText phoneEdit;
	@ViewInject(id = R.id.passwordEdit)
	private EditText passEdit;
	@ViewInject(id = R.id.loginBut)
	private Button loginBut;
	private String phone;
	private String password;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_user_login);
		setExitActivityMap(StrUtils.EXIT_ACTIVITY_LOGINANDREG, this);
		init();

	}

	private void init() {
		// TODO Auto-generated method stub
		setTitle("登录");
		loginBut.setOnClickListener(this);
		regText.setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.user_login, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) {
		case R.id.regText:
			startActivity(null, UserRegisterActivity.class);
			break;
		case R.id.loginBut:
			phone = phoneEdit.getText().toString().trim();
			password = passEdit.getText().toString();
			if (CheckUtils.phoneIsValid(phone)
					&& CheckUtils.passIsValid(password)) {
				goLogin();
			} else {
				MToast.show(context, "输入有误，请重新输入");
			}
			break;
		default:
			break;
		}
	}

	private void goLogin() {
		// TODO Auto-generated method stub
		User user = new User();
		user.setUsername(phone);
		user.setPassword(MD5.getMd5Code(password));
		user.login(context, new SaveListener() {
			@Override
			public void onSuccess() {
				// TODO Auto-generated method stub
				MToast.show(context, "登录成功");
//				UserCache.isLogin = true;
				SessionToken.update(context);
				exitMap(StrUtils.EXIT_ACTIVITY_LOGINANDREG);
			}

			@Override
			public void onFailure(int arg0, String arg1) {
				// TODO Auto-generated method stub
				MToast.show(context, "登录失败," + arg0);
			}
		});
	}
}
